package com.jingyanzi.member.service.impl;

import java.util.ArrayList;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;

import com.jingyanzi.member.dao.MCustomerMergeMapper;
//import com.jingyanzi.member.domain.AccountDO;
import com.jingyanzi.member.domain.IntegralDO;
import com.jingyanzi.member.domain.IntegralLogDO;

import com.jingyanzi.member.domain.TagDO;
import com.jingyanzi.member.domain.TrackRecordDO;
import com.jingyanzi.member.domain.result.ResultDO;
import com.jingyanzi.member.entity.MCustomerMerge;
import com.jingyanzi.member.entity.MCustomerMergeExample;
import com.jingyanzi.member.entity.MIntegral;
import com.jingyanzi.member.entity.MIntegralLog;
import com.jingyanzi.member.entity.MTags;
import com.jingyanzi.member.entity.MTrackRecord;
import com.jingyanzi.member.util.BeanUtilsExtends;
import com.jingyanzi.privilege.domain.EmployeeDO;
import com.jingyanzi.privilege.domain.OrgDO;
import com.jingyanzi.privilege.factory.PrivilegeFactory;
import com.jingyanzi.privilege.service.IEmployeeService;
import com.jingyanzi.privilege.service.IOrgService;

/**
 * @author reagan
 * @Description:基础服务类主要用来实现底层的通用方法
 */
public class BaseServiceImpl {
	

	@Autowired
	private MCustomerMergeMapper customerMergeMapper;

	public List<Integer> createCustomerId(Integer customId) {

		MCustomerMergeExample ex = new MCustomerMergeExample();
		ex.createCriteria().andMaterIdEqualTo(customId);
		List<MCustomerMerge> cmList = customerMergeMapper.selectByExample(ex);
		List<Integer> inId = new ArrayList<Integer>();
		
		if(cmList.size() != 0) {
		
			for(MCustomerMerge cm : cmList) {
				inId.add(cm.getMerge());
			}
			inId.add(customId);
		} else {
			inId.add(customId);
		}
		
		return inId;
	}

	private IOrgService getOrgService() {
		return PrivilegeFactory.getOrgService();
	}

	private IEmployeeService getEmployeeService() {
		return PrivilegeFactory.getEmployeeService();
	}

	/** 
	 * 查询员工信息
	 *  
	 * */
	public EmployeeDO getEmployeeById(Integer id) {
		com.jingyanzi.privilege.domain.result.ResultDO empResultDO = getEmployeeService().getEmployee(id);
		if (!empResultDO.isSuccess()) {
			return null;
		}
		return (EmployeeDO) empResultDO.getModel(com.jingyanzi.privilege.domain.result.ResultSupport.FIRST_MODEL_KEY);
	}

	public String getEmployeeOrg(Integer orgId) {
		com.jingyanzi.privilege.domain.result.ResultDO res = getOrgService().getOrgReverseList(orgId);
		List<OrgDO> orgList = (List<OrgDO>) res.getModel(ResultDO.FIRST_MODEL_KEY);
		String orgName = "";
		if (orgList.size() == 0) {
			return null;
		}
		if (orgList.size() != 1) {
			for (OrgDO org : orgList) {
				orgName = org.getNameCode() + "/";
			}
			orgName = orgName.substring(0, orgName.length() - 1);
		} else {
			orgName = orgList.get(0).getNameCode();
		}
		return orgName;
	}

	/**
	 * 对象拷贝单个实体到对象中
	 * 
	 * @param obj
	 * @return 返回DO对象
	 *//*
	public AccountDO getAccountDO(MAccount obj) {
		if (obj == null)
			return null;
		AccountDO dst = new AccountDO();
		if(BeanUtilsExtends.copyProperties(dst, obj)) {
			if (dst != null) {
				dst.setCreatetime(String.valueOf(obj.getCreatetime().getTime()));
				if (obj.getDepositDate() != null) {
					dst.setDepositStamp(String.valueOf(obj.getDepositDate().getTime()));
				}
				if (obj.getPayDate() != null) {
					dst.setPayDateStamp(String.valueOf(obj.getPayDate().getTime()));
				}
				return dst;
			}
		}
		return null;
	}

	*//**
	 * 对象拷贝多个实体到列表中
	 * 
	 * @param List
	 * @return List
	 *//*
	public List<AccountDO> getAccountDOList(List<MAccount> list) {
		List<AccountDO> result = new ArrayList<AccountDO>();
		if (list != null && !list.isEmpty()) {
			for (MAccount org : list) {
				result.add(getAccountDO(org));
			}
		}
		return result;
	}*/

	
	/**
	 * 对象拷贝单个实体到对象中
	 * 
	 * @param obj
	 * @return 返回DO对象
	 */
	public IntegralDO getIntegralDO(MIntegral obj) {
		if (obj == null)
			return null;
		IntegralDO dst = new IntegralDO();
		return BeanUtilsExtends.copyProperties(dst, obj) ? dst : null;
	}

	/**
	 * 对象拷贝单个实体到对象中
	 * 
	 * @param obj
	 * @return 返回DO对象
	 */
	public IntegralLogDO getIntegralLogDO(MIntegralLog obj) {
		IntegralLogDO dst = new IntegralLogDO();
		if (obj == null)
			return dst;
		return BeanUtilsExtends.copyProperties(dst, obj) ? dst : null;
	}

	/**
	 * 对象拷贝多个实体到列表中
	 * 
	 * @param List
	 * @return List
	 */
	public List<IntegralLogDO> getIntegralLogDOList(List<MIntegralLog> list) {
		List<IntegralLogDO> result = new ArrayList<IntegralLogDO>();
		if (list != null && !list.isEmpty()) {
			for (MIntegralLog org : list) {
				IntegralLogDO org1 = getIntegralLogDO(org);
				org1.setCreateTimeName(String.valueOf(org.getCreatetime().getTime()));
				if (org1 != null) {
					result.add(org1);
				}
			}
		}
		return result;
	}

	/**
	 * 对象拷贝单个实体到对象中
	 * 
	 * @param obj
	 * @return 返回DO对象
	 */
	public TagDO getTagDO(MTags obj) {
		if (obj == null)
			return null;
		TagDO dst = new TagDO();
		return BeanUtilsExtends.copyProperties(dst, obj) ? dst : null;
	}

	public TrackRecordDO getTrackRecordDO(MTrackRecord obj) {
		if (obj == null)
			return null;
		TrackRecordDO dst = new TrackRecordDO();
		dst = BeanUtilsExtends.copyProperties(dst, obj) ? dst : null;
		dst.setCreateTimeName(String.valueOf(obj.getCreatetime().getTime()));
		if (obj.getPlanTime() != null) {
			dst.setPlanTimeStamp(String.valueOf(obj.getPlanTime().getTime()));
		}
		return dst;
	}

	public List<TrackRecordDO> getTrackRecordDOList(List<MTrackRecord> list) {
		List<TrackRecordDO> result = new ArrayList<TrackRecordDO>();
		if (list != null && !list.isEmpty()) {
			for (MTrackRecord org : list) {
				TrackRecordDO org1 = getTrackRecordDO(org);
				if (org1 != null) {
					result.add(org1);
				}
			}
		}
		return result;
	}

	
	/**
	 * 判断整型数据是否无效： null或<1为无效,返回true; >=1为有效，返回false
	 */
	public static boolean isNumberInValid(Integer value) {
		if (value == null) {
			return true;
		} else if (value < 1) {
			return true;
		}
		return false;
	}

}
